# Maintainer: Andreas 'Segaja' Schleifer <segaja at archlinux dot org>

_gemname='erubi'
pkgname="ruby-${_gemname}"
pkgver=1.11.0
pkgrel=1
pkgdesc='Small ERB Implementation'
arch=('any')
url="https://github.com/jeremyevans/erubi"
license=('MIT')
depends=('ruby')
makedepends=('ruby-rake' 'ruby-rdoc')
checkdepends=('ruby-minitest' 'ruby-minitest-global_expectations')
options=(!emptydirs)
source=("${url}/archive/${pkgver}/${pkgname}-${pkgver}.tar.gz")
sha512sums=('6e1a044a8468b6cd0262e4058a45415b9a4e1f230bbd7256956d5416674feb4af6a1dea290733a3dbaeab92394625664a1e61cbe36e7278e1e030c6a9b16d692')

prepare() {
  cd "${_gemname}-${pkgver}"

  # update gemspec/Gemfile to allow newer version of the dependencies
  sed --in-place --regexp-extended 's|~>|>=|g' "${_gemname}.gemspec"
}

build() {
  cd "${_gemname}-${pkgver}"

  gem build "${_gemname}.gemspec"
}

check() {
  cd "${_gemname}-${pkgver}"

  rake spec
}

package() {
  cd "${_gemname}-${pkgver}"

  local _gemdir="$(gem env gemdir)"

  gem install --ignore-dependencies --no-user-install --install-dir "${pkgdir}/${_gemdir}" --bindir "${pkgdir}/usr/bin" "${_gemname}-${pkgver}.gem"

  # remove unrepreducible files
  rm --force --recursive --verbose \
    "${pkgdir}/${_gemdir}/cache/" \
    "${pkgdir}/${_gemdir}/gems/${_gemname}-${pkgver}/vendor/" \
    "${pkgdir}/${_gemdir}/doc/${_gemname}-${pkgver}/ri/ext/"

  find "${pkgdir}/${_gemdir}/gems/" \
    -type f \
    \( \
      -iname "*.o" -o \
      -iname "*.c" -o \
      -iname "*.so" -o \
      -iname "*.time" -o \
      -iname "gem.build_complete" -o \
      -iname "Makefile" \
    \) \
    -delete

  find "${pkgdir}/${_gemdir}/extensions/" \
    -type f \
    \( \
      -iname "mkmf.log" -o \
      -iname "gem_make.out" \
    \) \
    -delete

  install -Dm 644 MIT-LICENSE "${pkgdir}/usr/share/licenses/${pkgname}/LICENSE"
  install -Dm 644 CHANGELOG README.rdoc --target-directory "${pkgdir}/usr/share/doc/${pkgname}"
}
